package com.hankarun.gevrek.lib;

import java.io.UnsupportedEncodingException;

import org.apache.commons.net.nntp.Article;
import org.apache.james.mime4j.codec.DecodeMonitor;
import org.apache.james.mime4j.codec.DecoderUtil;

import android.view.View;


public class Tuple2{
	public Article article;
	public String head;
	public String parsedFrom;
	public int visible;
	public String bodys;
	
	public boolean bodyretrived;
	
	private void update(){
		visible = View.GONE;
		bodyretrived = false;
		article.setFrom(decodeString(article.getFrom()));
		parsedFrom = article.getFrom();
		if(parsedFrom != null){
			parsedFrom.trim();
	        if(parsedFrom.contains("(")){		            	
	        	parsedFrom = parsedFrom.substring(parsedFrom.indexOf('(')+1, parsedFrom.indexOf(')'));		            	
	        }else{
	        	parsedFrom = parsedFrom.substring(0,parsedFrom.indexOf('<'));
	        	parsedFrom.trim();
	        }
		}
	}
	
	public void setArticle(Article _article){
		article = _article;
		update();
	}
	
	public Tuple2(Article _article,String _head){
		article = _article;
		head = _head;
		update();
	}
	
	@Override
	public String toString(){
		return head+" "+decodeString(article.getSubject());
	}
	
	private String decodeString(String s){
		if(s == null)
			return null;
		if (s.indexOf("=?") != -1) {
			return DecoderUtil.decodeEncodedWords(s,DecodeMonitor.SILENT);
		}
		try{
			return new String(s.getBytes("ISO8859-9"), "ISO8859-9");
		} catch (UnsupportedEncodingException e) {

		}
		return s;        		
	}
}
